﻿using Acme.BookStore.RoomNumbers;
using Acme.BookStore.RoomTable;
using HIS;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.RoomPricess
{
    [ApiExplorerSettings(GroupName = "v1")]
    public class RoomPriceServices : ApplicationService, IRoomPriceServices
    {
        public RoomPriceServices(IRepository<RoomPrices> RoomPricesrepository,IRepository<RoomType> RoomTyperepository)
        {
            this.RoomPricesrepository = RoomPricesrepository;
            this.RoomTyperepository = RoomTyperepository;
        }

        public IRepository<RoomPrices> RoomPricesrepository { get; }
        public IRepository<RoomType> RoomTyperepository { get; }

        /// <summary>
        /// 房价添加
        /// </summary>
        /// <param name="roomPriceAddDTO"></param>
        /// <returns></returns>
        [HttpPost("api/RoomPrice")]
        public async Task<APIResult> RoomPriceAdd(RoomPriceAddDTO roomPriceAddDTO)
        {
            try
            {
                RoomPrices roomprices = ObjectMapper.Map<RoomPriceAddDTO,RoomPrices>(roomPriceAddDTO);
                await RoomPricesrepository.InsertAsync(roomprices);
                if (roomprices == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "添加失败"
                    };
                }
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "房号添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房价显示
        /// </summary>
        /// <param name="RoomTypeId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("api/RoomPrice")]
        public async Task<APIResult<List<RoomPriceDTO>>> RoomPriceShow(int RoomTypeId)
        {
            try
            {
                var roomprice = await RoomPricesrepository.GetListAsync();
                var roomtype = await RoomTyperepository.GetListAsync();
                var list = from a in roomprice
                           join b in roomtype on a.RoomTypeId equals b.Id
                           select new RoomPriceDTO
                           {
                               Id = a.Id,
                               RoomTypeId = a.RoomTypeId,
                               ProductName = a.ProductName,
                               Price = a.Price,
                               PayoutMethod = a.PayoutMethod,
                               AccommodationOffers = a.AccommodationOffers,
                               MembershipSpread = a.MembershipSpread,
                               BreakfastNumber = a.BreakfastNumber,
                               RoomNumber = a.RoomNumber,
                               SalesStrategy = a.SalesStrategy,
                               RoomStatus = a.RoomStatus,
                               RoomSort = a.RoomSort,
                               RoomTypeName = b.RoomName
                           };
                
                if (RoomTypeId != 0)
                {
                    list = list.Where(x => x.RoomTypeId == RoomTypeId);
                }
                return new APIResult<List<RoomPriceDTO>>()
                {
                    Code = CodeEnum.success,
                    Message = "查询成功",
                    Data = list.ToList()
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房价修改状态
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("api/RoomPriceStatus")]
        public async Task<APIResult> RoomPriceStatus(int Id)
        {
            try
            {
                var roomprice = await RoomPricesrepository.FirstOrDefaultAsync(x => x.Id == Id);
                if (roomprice == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "修改失败，未找到指定的房型"
                    };
                }
                roomprice.RoomStatus = !roomprice.RoomStatus;
                await RoomPricesrepository.UpdateAsync(roomprice);
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "修改成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房价修改
        /// </summary>
        /// <param name="roomPriceUpdateDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("api/RoomPrice")]
        public async Task<APIResult> RoomPriceUpdate(RoomPriceUpdateDTO roomPriceUpdateDTO)
        {
            try
            {
                var roomprice = await RoomPricesrepository.FirstOrDefaultAsync(x => x.Id == roomPriceUpdateDTO.Id);
                if (roomprice == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "房型未找到"
                    };
                }

                ObjectMapper.Map(roomPriceUpdateDTO, roomprice);
                await RoomPricesrepository.UpdateAsync(roomprice);

                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "房型修改成功"
                };
            }
            catch (Exception ex)
            {
                return new APIResult()
                {
                    Code = CodeEnum.error,
                    Message = $"修改失败: {ex.Message}"
                };
            }
        }
        /// <summary>
        /// 房价删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("api/RoomPirce")]
        public async Task<APIResult> RoomTypeSingleDelete(int Id)
        {
            try
            {
                var roomprice = await RoomPricesrepository.FirstOrDefaultAsync(x => x.Id == Id);
                if (roomprice == null)
                {
                    return new APIResult()
                    {
                        Code = CodeEnum.error,
                        Message = "删除失败，未找到指定的房型"
                    };
                }
                await RoomPricesrepository.DeleteAsync(roomprice);
                return new APIResult()
                {
                    Code = CodeEnum.success,
                    Message = "删除成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
